//
//  RigsiView.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/20.
//  Copyright © 2020 tym. All rights reserved.
//

#import "RegsiView.h"
#import "Sizes.h"
#import "Colors.h"
#import "UIimage+Extends.h"
#import "WHToast.h"
#import "UIButton+Gradient.h"
#import "UIView+ZFFrame.h"

@interface RegsiView()

@property(nonatomic,assign) NSInteger seconds;
@end

@implementation RegsiView

-(void)awakeFromNib{
    [super awakeFromNib];
    self.getCodeBtn.layer.cornerRadius = 3;
    self.getCodeBtn.layer.borderColor = rgba(74, 74, 74).CGColor;
    self.getCodeBtn.layer.borderWidth= 1.5;
    self.goBtn.layer.cornerRadius = 5;
    self.goBtn.layer.masksToBounds = YES;
    self.goBtn.zf_width = ScreenWidth - 60;
    _seconds = 60;
    [self changedTextField:nil];
    [self.phoneFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.passFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.codeFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
}

//开始倒计时
-(void)startCountDown{
    [self changeSecend];
}

-(void)changeSecend{
    if (_seconds > 0) {
        _seconds =_seconds-1;
        [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%lds",_seconds] forState:UIControlStateNormal];
        [self performSelector:@selector(changeSecend) withObject:self afterDelay:1];
    } else{
        [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
        _seconds = 60;
    }
}

-(void)changedTextField:(UITextField*)fid{
    if (self.phoneFid.text.length < 11 || self.passFid.text.length<1 || self.codeFid.text.length < 4) {
        self.goBtn.selected = NO;
        [self.goBtn setBackgroundImage:[UIImage imageWithColor:rgba(230, 230, 230) withSize:self.goBtn.bounds.size] forState:UIControlStateNormal];
    }else{
        self.goBtn.selected = YES;
        [self.goBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    }
}

-(IBAction)getCodeAction:(id)sender{
    
    if(_seconds < 60){
        return;
    }
    
    if (self.getCodeBlock) {
        self.getCodeBlock(self.phoneFid.text);
    }
}

-(IBAction)goRegisterAction:(id)sender{
    
    if (!self.goBtn.selected) {
        return;
    }
    
    if (![self checkPassword:self.passFid.text]) {
        [WHToast showMessage:@"密码长度6-20位，至少包含字母，数字和标点符号中的两种类型" duration:2 finishHandler:nil];
        return;
    }
    
    if (self.phoneFid.text.length < 11) {
        [WHToast showMessage:@"请检查您输入的手机号码" duration:2 finishHandler:nil];
        return;
    }
    
    if (self.passFid.text.length < 1) {
        [WHToast showMessage:@"请输入密码" duration:2 finishHandler:nil];
        return;
    }
    
    if (self.codeFid.text.length < 4) {
        [WHToast showMessage:@"验证码输入有误" duration:2 finishHandler:nil];
        return;
    }
    
    
    if (self.registerBlock) {
        self.registerBlock(self.phoneFid.text, self.passFid.text, self.codeFid.text);
    }
    
}

-(IBAction)doChangePasswordIamge:(UIButton*)sender{
    if (sender.isSelected) {
        [sender setImage:[UIImage imageNamed:@"icon_login9"] forState:UIControlStateNormal];
        [self.passFid setSecureTextEntry:NO];
    }else{
        [sender setImage:[UIImage imageNamed:@"icon_login6"] forState:UIControlStateNormal];
        [self.passFid setSecureTextEntry:YES];
    }
    sender.selected = !sender.selected;
}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
    [self.passFid endEditing:YES];
    [self.phoneFid endEditing:YES];
    [self.codeFid endEditing:YES];
}


//检测密码
-(BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString *blank = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
    if(![string isEqualToString:blank]) {
        return NO;
    }
    if (textField.text.length >= 11 && ![string isEqualToString:@""]) {
        return NO;
    }
    return YES;
}
@end
